﻿using DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace DAL.DBManagers
{
    public class UsersManager : BaseManager<User>
    {
        public UsersManager()
            : base()
        {
        }

        public override IEnumerable<User> GetAll()
        {
            return base.GetAll().Where(w => w.IsActive).ToList();
        }

        public void Delete(Guid UserID)
        {
            var obj = base.Single(s => s.UserID == UserID);
            obj.IsActive = false;
            obj.LastModifyDate = DateTime.Now;
            //obj.LastModifier = HttpContext.Current.Session[0];
            //TODO : lastmodifier

            base.SaveChanges();
        }

        public bool Authentifacation(out UserHelper user, string username, string password)
        {
            var result = base.SingleOrDefault(s => s.UserName == username && s.Password == password && s.IsActive);

            if (result != null)
            {
                user = new UserHelper
                {
                    UserID = result.UserID,
                    UserName = username,
                    FullName = result.FullName,
                    Permissions = result.UserPermissions.Where(w => w.IsActive && w.Permission.IsActive).Select(s => s.Permission.Name).ToList<string>()
                };

                return true;
            }
            else
            {
                user = null;
                return false;
            }
        }
    }
}
